class ListNode {
    int val;
    ListNode next;
    ListNode(int val) {
        this.val = val;
    }
    ListNode(int val, ListNode next) {
        this.val = val;
            this.next = next;
     }

  }

class Solution {
    public ListNode reverseList(ListNode head) {
        if (head==null){
            return null;
        }

        if (head.next==null){
            return head;
        }
        ListNode prev = new ListNode(0);
        prev.next = head;
        head=prev;
        ListNode cur = head.next;
        head = null;
        while(cur!=null){
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }
}


public class Text1 {
    public static void main(String[] args) {

    }
}
